/**
 * Description:
 * Class Name:subject3
 * Date:2022/2/24 18:55
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第7章_Java基础类库.课后练习;

import java.util.Arrays;
import java.util.regex.*;

public class subject3 {
    public void parse(String target_string, int[] int_list, String[] string_list){

    }

    public static void main(String[] args) {
        String target_string = "A1B2C3D4E5F6G7H8";

        Pattern p_int = Pattern.compile("\\d");
        Pattern p_alpha = Pattern.compile("\\D");
        Matcher m_int = p_int.matcher(target_string);
        Matcher m_alpha = p_alpha.matcher(target_string);
        int int_count = 0;
        int alpha_count = 0;
        while (m_int.find()){
            int_count++;
        }
        while (m_alpha.find()){
            alpha_count++;
        }
        int[] result_int_list = new int[int_count];
        String[] result_string_list = new String[alpha_count];
        int temp = 0;
        m_int.reset(target_string);     // 迭代完毕后若想重新对其循环，则需使用reset
        while (m_int.find()){
            result_int_list[temp++] = Integer.parseInt(m_int.group());
        }
        m_alpha.reset(target_string);
        for (int i = 0; i < alpha_count; i++) {
            if (m_alpha.find()){
                result_string_list[i] = m_alpha.group();
            }
        }
        System.out.println("int数组为：" + Arrays.toString(result_int_list));
        System.out.println("alpha数组为：" + Arrays.toString(result_string_list));
    }
}



//import java.util.Arrays;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//public class subject3 {
//    public static void main(String[] args)
//    {      //根据题目要求定义字符串
//        String str = "A1B2C3D4E5F6G7H8";
//        int i = 0;
//        int a = 0;
//        //定义两个数组存放拆分出来的数字,注意下标越界问题
//        int[] shu = new int[8];
//        String[] s1 = new String[8];
//        //Matcher类提供的方法,find():返回目标字符串中是包含与Pattern匹配的字符串,group返回上一次与Pattern匹配的字符串
//        Matcher m = Pattern.compile(("\\d")).matcher(str);   //利用正则表达式支持的预定义字符: \\d匹配0-9的所有数字
//        System.out.println("原来的字符串等于:" + str);
//        System.out.print("拆分为:");
//        while (m.find())
//        {
//            System.out.print(m.group());
//            //将匹配到的符合要求的字符串存到数组中去,调用Integer的静态方法
//            shu[i++] = Integer.parseInt(m.group());
//        }
//        System.out.print("\n");
//        //将拆分好的放入数组,通过Arrays输出数组
//        System.out.println("放入int[]数组为:" + Arrays.toString(shu));
//        //这个是分割线
//        System.out.println("                                      ");
//        {                             //\\D是正则表达式匹配非数字
//            Matcher s = Pattern.compile(("\\D")).matcher(str);
//            System.out.println("原字符串等于:" + str);
//            System.out.print("拆分为:");
//            while (s.find())
//            {
//                System.out.print(s.group());
//                s1[a++] = s.group();
//            }
//            System.out.print("\n");
//            System.out.println("放入数组String[]为" + Arrays.toString(s1));
//        }
//    }
//}

